package util

import "time"

//毫秒的时间偏移量
var offset int64 = 0

var ONE_MINUTE_SECOND int64 = 60
var ONE_HOUR_SECOND int64 = ONE_MINUTE_SECOND * 60
var ONE_HOUR_MILLIS int64 = ONE_MINUTE_SECOND * 1000
var ONE_DAY_SECOND int64 = ONE_HOUR_SECOND * 24
var ONE_DAY_MILLIS int64 = ONE_DAY_SECOND * 1000

func Now() int64 {
	return time.Now().UnixMilli() + offset
}

//今天零点的时间
func TodayZeroMillis() int64 {
	millis := Now()
	now := time.UnixMilli(millis)
	todayZero := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
	return todayZero.UnixMilli()
}

//明天零点的时间
func TomorrowZeroMillis() int64 {
	return TodayZeroMillis() + ONE_DAY_MILLIS
}

//周一零点的时间
func WeekMondayZeroMillis() int64 {
	millis := TodayZeroMillis()
	now := time.UnixMilli(millis)
	weekday := now.Weekday()
	if weekday == 0 {
		return millis - ONE_DAY_MILLIS*6
	} else {
		return millis - ONE_DAY_MILLIS*int64(weekday-1)
	}
}

//下周一零点的时间
func NextWeekMondayZeroMillis() int64 {
	return WeekMondayZeroMillis() + ONE_DAY_MILLIS*7
}

//本月零点的时间
func MonthZeroMillis() int64 {
	millis := Now()
	now := time.UnixMilli(millis)
	monthZero := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
	return monthZero.UnixMilli()
}

//下月零点的时间
func NextMonthZeroMillis() int64 {
	millis := Now()
	now := time.UnixMilli(millis)
	monthZero := time.Date(now.Year(), now.Month()+1, 1, 0, 0, 0, 0, now.Location())
	return monthZero.UnixMilli()
}

//某个时间点当天的零点
func SomedayZeroMillis(timeMillis int64) int64 {
	theTime := time.UnixMilli(timeMillis)
	zeroTime := time.Date(theTime.Year(), theTime.Month(), theTime.Day(), 0, 0, 0, 0, theTime.Location())
	return zeroTime.UnixMilli()
}
